package com.shu.mmall.controller.backend;

import com.shu.mmall.common.Const;
import com.shu.mmall.common.ResponseCode;
import com.shu.mmall.common.Result;
import com.shu.mmall.pojo.User;
import com.shu.mmall.service.ICategoryService;
import com.shu.mmall.service.IUserService;
import com.shu.mmall.util.EmptyUtil;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;

/**
 * @author shujinhao
 */
@RequestMapping("/category/manager/")
@Controller
public class CategoryManagerController {
    @Autowired
    ICategoryService categoryService;
    @Autowired
    IUserService userService;

    @RequestMapping(value = "addCategory.do",method = RequestMethod.POST)
    @ResponseBody
    public Result addCategory(HttpSession session,String categoryName,@RequestParam(value = "parentId",defaultValue = "0") int parentId){
        User user=(User)session.getAttribute(Const.CURRENT_USER);
        if (EmptyUtil.isNull(user)){
            return Result.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),"用户未登录，请登录");
        }
        Result result=userService.checkAdminRole(user);
        if (result.isSuccess()){
            //是管理员
            return categoryService.addCategory(categoryName,parentId);
        }else {
            return Result.createByErrorMessage("无权限操作，需要管理员权限");
        }
    }
    @RequestMapping(value = "updateCategoryName.do",method = RequestMethod.POST)
    @ResponseBody
    public Result updateCategoryName(HttpSession session,Integer categoryId,String categoryName){
        User user=(User)session.getAttribute(Const.CURRENT_USER);
        if (EmptyUtil.isNull(user)){
            return Result.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),"用户未登录，请登录");
        }
        Result result=userService.checkAdminRole(user);
        if (result.isSuccess()){
            //是管理员
            return categoryService.updateCategoryName(categoryName,categoryId);
        }else {
            return Result.createByErrorMessage("无权限操作，需要管理员权限");
        }

    }
    @RequestMapping(value = "getChildrenParallelCategory.do",method = RequestMethod.POST)
    @ResponseBody
    public Result getChildrenParallelCategory(HttpSession session,@RequestParam(value = "categoryId",defaultValue = "0") Integer categoryId){
        User user=(User)session.getAttribute(Const.CURRENT_USER);
        if (EmptyUtil.isNull(user)){
            return Result.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),"用户未登录，请登录");
        }
        Result result=userService.checkAdminRole(user);
        if (result.isSuccess()){
            //是管理员,查询子节点的category信息，并且不递归，保持平级
            return categoryService.getChildrenParallelCategory(categoryId);
        }else {
            return Result.createByErrorMessage("无权限操作，需要管理员权限");
        }
    }
    @RequestMapping(value = "getCategoryAndDeepChildrenCategory.do",method = RequestMethod.POST)
    @ResponseBody
    public Result getCategoryAndDeepChildrenCategory(HttpSession session,@RequestParam(value = "categoryId",defaultValue = "0") Integer categoryId){
        User user=(User)session.getAttribute(Const.CURRENT_USER);
        if (EmptyUtil.isNull(user)){
            return Result.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),"用户未登录，请登录");
        }
        Result result=userService.checkAdminRole(user);
        if (result.isSuccess()){
            //是管理员
            return categoryService.getCategoryAndDeepChildrenCategory(categoryId);
        }else {
            return Result.createByErrorMessage("无权限操作，需要管理员权限");
        }
    }


}
